/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.yuecai.modules.yc_goods_sku.service;


import com.yuecai.modules.yc_goods.domain.YcGoods;
import com.yuecai.modules.yc_goods.repository.YcGoodsRepository;
import com.yuecai.modules.yc_goods_sku.domain.YcGoodsSku;
import com.yuecai.modules.yc_goods_sku.repository.YcGoodsSkuRepository;
import com.yuecai.modules.yc_goods_sku.service.dto.GoodsSkuDto;
import com.yuecai.modules.yc_goods_sku.service.dto.YcGoodsSkuDto;
import com.yuecai.modules.yc_goods_sku.service.dto.YcGoodsSkuQueryCriteria;
import com.yuecai.modules.yc_goods_sku.service.mapstruct.YcGoodsSkuMapper;
import com.yuecai.utils.FileUtil;
import com.yuecai.utils.PageUtil;
import com.yuecai.utils.QueryHelp;
import com.yuecai.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author dm
* @date 2020-06-29
**/
@Service
@RequiredArgsConstructor
public class YcGoodsSkuService {

    private final YcGoodsSkuRepository ycGoodsSkuRepository;
    private final YcGoodsRepository ycGoodsRepository;
    private final YcGoodsSkuMapper ycGoodsSkuMapper;

    
    public Map<String,Object> queryAll(YcGoodsSkuQueryCriteria criteria, Pageable pageable){
        Page<YcGoodsSku> page = ycGoodsSkuRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);

        return PageUtil.toPage(page.map(ycGoodsSkuMapper::toDto));
    }


    
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity<Object> create(GoodsSkuDto resources) throws Exception {
        Long goodId = resources.getGoodId();
        Optional<YcGoods> byId = ycGoodsRepository.findById(goodId);
        if (byId.isPresent()){
            YcGoods ycGoods = byId.get();
            YcGoodsSku sku = new YcGoodsSku();
            BeanUtils.copyProperties(resources,sku);
            sku.setYcGoods(ycGoods);
            ycGoodsSkuRepository.save(sku);
            return ResponseEntity.ok("添加成功");
        }else {
            throw new Exception("商品不存在！");
        }
    }

    
    @Transactional(rollbackFor = Exception.class)
    public void update(GoodsSkuDto resources) throws Exception {
        Long goodId = resources.getGoodId();
        Optional<YcGoods> byId = ycGoodsRepository.findById(goodId);
        if (byId.isPresent()){
            YcGoods ycGoods = byId.get();
            YcGoodsSku sku = new YcGoodsSku();
            BeanUtils.copyProperties(resources,sku);
            sku.setYcGoods(ycGoods);
            ycGoodsSkuRepository.save(sku);
        }else {
            throw new Exception("商品不存在！");
        }
    }

    
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            ycGoodsSkuRepository.deleteById(id);
        }
    }

    
    public void download(List<YcGoodsSkuDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YcGoodsSkuDto ycGoodsSku : all) {
            Map<String,Object> map = new LinkedHashMap<>();

            map.put("规格", ycGoodsSku.getSpecValues());
            map.put("库存", ycGoodsSku.getStock());
            map.put("是否限购，", ycGoodsSku.getIsForPurchasin());
            map.put("限购", ycGoodsSku.getForPurchasing());
            map.put("原价", ycGoodsSku.getOriginalPrice());
            map.put("销售价", ycGoodsSku.getSellPrice());
            map.put("缩略图", ycGoodsSku.getImage());
            map.put("销量", ycGoodsSku.getSales());
            map.put("创建时间", ycGoodsSku.getCreateTime());
            map.put("更新时间", ycGoodsSku.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}